﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Enterprise.Infrastructure;
using Enterprise.Model.Meeting;
using Enterprise.Service.Meeting;
using Enterprise.Control;
using Enterprise.Service.Department;
using Enterprise.Service.User;
namespace Enterprise.UI.Web.Modules.Meeting
{
    public partial class Edit : Enterprise.Service.BasePage
    {
        protected int Id = (int)Utility.sink("Id", Utility.MethodType.Get, 0, 0, Utility.DataType.Int);
        string Cmd = (string)Utility.sink("Cmd", Utility.MethodType.Get, 0, 0, Utility.DataType.Str);
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                OnStart();
            }
        }

        /// <summary>
        /// 初始化加载项
        /// </summary>
        private void OnStart()
        {            
            if (Cmd == "Edit")
            {
                OnBindData();
            }
            else if (Cmd == "Delete")
            {
                MeetingService mService = new MeetingService();
                var q = mService.MeetingDisp(Id);
                if (q != null)
                {
                    string _note = "操作成功";
                    q.DB_Option_Action = "Delete";
                    try
                    {
                        mService.MeetingExecute(q);
                    }
                    catch (Exception ex)
                    {
                        _note = ex.Message;
                    }
                    Utility.ShowMsg(this.Page, "OK", this.Tran(_note), "Index.aspx");
                }
            }
            else
            {
                int deptId = 0;
                lb_User.Text = UserService.GetUserName(Utility.Get_UserId, out deptId);
                lb_Dept.Text = DepartmentService.GetDepartMentName(deptId);
                t_CreateTime.Text = DateTime.Now.ToString();
                t_Time.Text = DateTime.Now.ToString();
                CreateBT.AddButton("back.gif", "返回", "Index.aspx", Utility.PopedomType.List, HeadMenu1);
            }            
        }

        /// <summary>
        /// 当编辑时绑定数据项
        /// </summary>
        private void OnBindData()
        {
            MeetingService mService = new MeetingService();
            var q = mService.MeetingDisp(Id);
            if (q != null)
            {
                lb_User.Text = UserService.GetUserName(q.MUserId);
                lb_Dept.Text = DepartmentService.GetDepartMentName(q.MDepartMent);
                t_Address.Text = q.MAddress;
                t_Time.Text = q.MTime.ToString();
                t_Zcr.Text = q.MZcr;
                t_CreateTime.Text = q.MCreateTime.ToString();
                t_Title.Text = q.MTitle;
                t_Title_Ru.Text = q.MTitleRu;
                t_Content.Text = q.MContent;
                t_ContentRu.Text = q.MContentRu;
                rcv_users_value.Value = q.MUsers;
            }
        }

        protected void BtnSave_OnClick(object sender, EventArgs e)
        {
            string _note = "操作成功";
            try
            {
                MeetingService mService = new MeetingService();
                MeetingEntity entity = new MeetingEntity();
                if (string.IsNullOrEmpty(Cmd))
                {
                    entity.DB_Option_Action = "Insert";
                    entity.MUser = Utility.Get_UserId;
                    entity.MDepartMent = UserService.GetUserDeptId(Utility.Get_UserId);
                    entity.MUserId = Utility.Get_UserId;
                    entity.MCreateTime = DateTime.Now;
                }
                else if (Cmd == "Edit")
                {
                    entity = mService.MeetingDisp(Id);
                    entity.DB_Option_Action = "Update";
                }
                entity.MAddress = (string)Utility.sink(t_Address.UniqueID, Utility.MethodType.Post, 0, 0, Utility.DataType.Str);
                entity.MTime = (DateTime)Utility.sink(t_Time.UniqueID, Utility.MethodType.Post, 0, 0, Utility.DataType.Date);
                entity.MZcr = (string)Utility.sink(t_Zcr.UniqueID, Utility.MethodType.Post, 0, 0, Utility.DataType.Str);
                entity.MTitle = (string)Utility.sink(t_Title.UniqueID, Utility.MethodType.Post, 0, 0, Utility.DataType.Str);
                entity.MTitleRu = (string)Utility.sink(t_Title_Ru.UniqueID, Utility.MethodType.Post, 0, 0, Utility.DataType.Str);
                entity.MContent = (string)Utility.sink(t_Content.UniqueID, Utility.MethodType.Post, 0, 0, Utility.DataType.Str);
                entity.MContentRu = (string)Utility.sink(t_ContentRu.UniqueID, Utility.MethodType.Post, 0, 0, Utility.DataType.Str);
                entity.MFiles = "";
                //签收范围格式@1,4,5
                string rcv_users = rcv_users_value.Value;
                rcv_users = rcv_users.TrimEnd(',');
                entity.MUsers = rcv_users;
                mService.MeetingExecute(entity);
                if (entity.DB_Option_Action == "Insert")
                {
                    Enterprise.Service.Rtx.RtxService.SendRtxMessageServices(entity.MUsers, "办公事务 - 参会通知", "参会通知，时间:" + entity.MTime + ", 地点:" + entity.MAddress + ",主题:" + entity.MTitle + "", false);
                }
            }
            catch (Exception ex)
            {
                _note = ex.Message;
            }
            Utility.ShowMsg(this.Page, "OK", this.Tran(_note), "Index.aspx");
        }

    }
}